package com.hs.utils;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.InputStream;
import java.util.Properties;

public class MailUtils {
    //声明属性对象
    private static Properties properties;
    private static String host;
    private static String protocol;
    private static String auth;
    private static String from;
    private static String code;
    static{
        try{
            //加载属性文件
            properties = new Properties();
            InputStream in = Password.class.getClassLoader().getResourceAsStream("javamail.properties");
            properties.load(in);
            //获取属性文件中的值
            host = properties.getProperty("mail.host");
            protocol = properties.getProperty("mail.transport.protocol");
            auth = properties.getProperty("mail.smtp.auth");
            from = properties.getProperty("mail.from");
            code = properties.getProperty("mail.auth.code");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("密码属性文件加载失败");
        }
    }

    /**
     * 发送邮件
     * @param to
     * @param title
     * @param content
     * @throws Exception
     */
    public static void send(String to,String title,String content) throws Exception{
        //生成系统属性对象
        Properties prop = new Properties();
        prop.setProperty("mail.host", host);
        prop.setProperty("mail.transport.protocol", protocol);
        prop.setProperty("mail.smtp.auth", auth);
        Session session = Session.getInstance(prop);
        session.setDebug(true);
        //获取Transport对象
        Transport ts = session.getTransport();
        //连接邮箱服务器
        ts.connect("smtp.qq.com",from,code);
        Message message = createSimpleMail(session,to,title,content);
        ts.sendMessage(message, message.getAllRecipients());
    }

    /**
     * 发送简单邮件（不包括图片和附件）
     * @param session
     * @param to
     * @return
     * @throws Exception
     */
    public static MimeMessage createSimpleMail(Session session, String to, String title, String content) throws Exception{
        MimeMessage message = new MimeMessage(session);
        //设置发送邮箱地址
        message.setFrom(new InternetAddress(from));
        //设置目标邮箱地址
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        //设置标题
        message.setSubject(title);
        //设置邮件内容
        message.setContent(content, "text/html;charset=UTF-8");
        return message;
    }

    /**
     * 发送验证码
     * @param to
     * @return
     * @throws Exception
     */
    public static int sendVerificationCode(String to) throws Exception {
        //邮件标题
        String title = "在线考试系统";
        //生成6位随机数
        int code = (int)((Math.random()*9+1)*100000);
        //邮件内容
        String content = "尊敬的用户：您本次的验证码为："+code+"，请输入该验证码完成注册，验证码10分钟内有效";
        send(to,title,content);
        return code;
    }

    /**
     * 重置密码
     * @param to
     * @return
     * @throws Exception
     */
    public static Integer sendPassword(String to) throws Exception{
        //邮件标题
        String title = "在线考试系统";
        //生成6位随机数
        int password = (int)((Math.random()*9+1)*100000);
        //邮件内容
        String content = "尊敬的用户：您密码已重置为："+password+"，请尽快修改密码！";
        //发送邮件
        send(to,title,content);
        return password;
    }

    public static void main(String[] args) {
        try {
            int code = sendVerificationCode("2960787865@qq.com");
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}
